<template>
    <div>
         <!-- 面包屑导航 -->
        <el-breadcrumb separator-class="el-icon-arrow-right">
            <el-breadcrumb-item :to="{ path: '/welcome' }">首页</el-breadcrumb-item>
            <el-breadcrumb-item>商品管理</el-breadcrumb-item>
            <el-breadcrumb-item>商品分类</el-breadcrumb-item>
        </el-breadcrumb>
        <!-- 内容卡片 -->
        <el-card>
            <el-row>
                <el-col>
                    <el-button type="primary" @click="addCate">添加分类</el-button>
                </el-col>
            </el-row>
            <!-- 内容表格 -->
            <table-tree class="table-tree" :data="cateData" :columns="columns" show-index index-text="#" border :selection-type="false" :expand-type="false" :show-row-hover="false">
                <template slot="isOK" slot-scope="scope">
                    <!-- {{scope.row}} -->
                    <i class="el-icon-error" style="color: lightred" v-if="scope.row.cat_deleted"></i>
                    <i class="el-icon-success" style="color: lightgreen" v-else></i>
                </template>
                <template slot="level" slot-scope="scope">
                    <el-tag v-if="scope.row.cat_level == 0">一级</el-tag>
                    <el-tag type="success" v-else-if="scope.row.cat_level == 1">二级</el-tag>
                    <el-tag type="warning" v-else>三级</el-tag>
                </template>
                <template slot="operation">
                    <el-button type="primary" icon="el-icon-edit" size="mini">编辑</el-button>
                    <el-button type="danger" icon="el-icon-delete" size="mini">删除</el-button>
                </template>
            </table-tree>
            <!-- 分页功能 -->
            <el-pagination @size-change="handleSizeChange" @current-change="handleCurrentChange" :current-page="queryInfo.pagenum" :page-sizes="[3, 5, 10, 15]" :page-size="queryInfo.pagesize" layout="total, sizes, prev, pager, next, jumper" :total="total"></el-pagination>
        </el-card>
        <!-- 添加分类的弹窗 -->
        <el-dialog title="添加分类" :visible.sync="addCateVisible" width="50%" @close="addCateClose">
            <el-form :model="addCateForm" :rules="addCateRules" ref="addCateRef" label-width="100px" class="demo-ruleForm">
                <el-form-item label="分类名称" prop="cat_name">
                    <el-input v-model="addCateForm.cat_name"></el-input>
                </el-form-item>
                <el-form-item label="父级分类">
                    <el-cascader v-model="selectKeys" :options="parentCateLists" :props="parentCateProps" @change="selectChange" clearable change-on-select></el-cascader>
                </el-form-item>
            </el-form>
            <span slot="footer">
                <el-button @click="addCateVisible = false">取 消</el-button>
                <el-button type="primary" @click="subParentCate">确 定</el-button>
            </span>
        </el-dialog>
    </div>
</template>

<script>
export default {
    data() {
        return {
            cateLists: [],
            queryInfo: {
                type: 3,
                pagenum: 1,
                pagesize: 5
            },
            total: 0,
            cateData: [],
            columns: [{
                label: '分类名称',
                prop: 'cat_name'
            }, {
                label: '是否有效',
                type: 'template',
                template: 'isOK'
            }, {
                label: '级别',
                type: 'template',
                template: 'level'
            }, {
                label: '操作',
                type: 'template',
                template: 'operation'
            }],
            addCateVisible: false,
            addCateForm: {
                cat_name: '',
                cat_pid: 0,
                cat_level: 0
            },
            addCateRules: {
                cat_name: { required: true, message: '请输入分类名称', trigger: 'blur' }
            },
            parentCateLists: [],
            parentCateProps: {
                value: 'cat_id',
                label: 'cat_name',
                children: 'children',
                expandTrigger: 'hover'
            },
            selectKeys: []
        }
    },
    created() {
        this.getCateLists()
    },
    methods: {
        // 获取商品分类列表
        async getCateLists() {
            const { data: res } = await this.$http.get('categories', { params: this.queryInfo })
            this.cateData = res.data.result
            this.total = res.data.total
        },
        // 改变当前页面显示数据大小的方法
        handleSizeChange(val) {
            this.queryInfo.pagesize = val
            this.getCateLists()
        },
        // 改变当前页数的方法
        handleCurrentChange(val) {
            this.queryInfo.pagenum = val
            this.getCateLists()
        },
        // 添加分类事件
        addCate() {
            this.getParentCate()
            this.addCateVisible = true
        },
        // 关闭添加分类的弹窗
        addCateClose() {
            this.$refs.addCateRef.resetFields()
            this.selectKeys = []
            this.addCateForm.cat_level = 0
            this.addCateForm.cat_pid = 0
        },
        // 获取父级分类
        async getParentCate() {
            const { data: res } = await this.$http.get('categories', { params: { type: 2 } })
            if (res.meta.status !== 200) return this.$message.error('获取数据失败')
            this.parentCateLists = res.data
        },
        // 改变选择的父级分类的方法
        selectChange() {
            // console.log(this.selectKeys)
            if (this.selectKeys.length > 0) {
                this.addCateForm.cat_pid = this.selectKeys[this.selectKeys.length - 1]
                this.addCateForm.cat_level = this.selectKeys.length
            } else {
                this.addCateForm.cat_pid = 0
                this.addCateForm.cat_level = 0
            }
        },
        // 上传添加的数据
        subParentCate() {
            console.log(this.addCateForm)
            this.$refs.addCateRef.validate(async valid => {
                if (!valid) return
                const { data: res } = await this.$http.post('categories', this.addCateForm)
                if (res.meta.status !== 201) return this.$message.error(res.meta.msg)
                this.$message.success('添加分类成功')
                this.getParentCate()
                this.addCateVisible = false
            })
        }
    }
}
</script>

<style lang="less" scoped>
    .table-tree {
        margin: 15px 0;
    }

    .el-cascader-panel {
        height: 250px;
    }

    .el-cascader {
        width: 100%;
    }
</style>